﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.APIs.NVR;

namespace Waker.Devices
{
    public partial class FormNVREdit : Base.FormBasePop
    {
        DataTable _DtSource;
        public FormNVREdit(int id)
        {
            InitializeComponent();
            this.Text += id < 1 ? "添加" : "修改";
            _DtSource = new BAL.BAL_Device().GetNVRByID(id);
        }

        private void FormNVREdit_Load(object sender, EventArgs e)
        {
            if (_DtSource.Rows.Count < 1)
            {
                this.BindingContext[_DtSource].AddNew();
                speNVR_Port.Value = 8000;
                txtNVR_UserName.Text = "admin";
                txtNVR_Pwd.Text = "12345";

                this.BindingContext[_DtSource].EndCurrentEdit();
            }
            else
            {
                ControlUtil.BindData(this.Controls, _DtSource);
            }
            if (!Session.IsNVRInited)
            {
                Session.IsNVRInited = HCNetSDK.NET_DVR_Init();
                HCNetSDK.NET_DVR_SetConnectTime(5000, 1);
            }
        }
        protected override void FormSave()
        {
            if (!TestConnection())
                return;

            var isok = new BAL.BAL_Device().SaveNVR(
                _DtSource.Rows[0]["NVR_ID"] == DBNull.Value ? 0 : Convert.ToInt32(_DtSource.Rows[0]["NVR_ID"])
                , txtNVR_Name.Text.Trim()
                , txtNVR_IP.Text
                , Convert.ToInt32(speNVR_Port.Value)
                , txtNVR_UserName.Text
                , txtNVR_Pwd.Text
                , txtRemark.Text
                );
            if (isok)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
                return;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            TestConnection();
        }
        bool TestConnection()
        {
            base.errorPop.ClearErrors();
            if (string.IsNullOrEmpty(txtNVR_Name.Text))
            {
                base.errorPop.SetError(txtNVR_Name, "设备名称不能为空");
                return false;
            }
            if (string.IsNullOrEmpty(txtNVR_IP.Text) || !IPHelper.CheckIPAddr(txtNVR_IP.Text))
            {
                base.errorPop.SetError(txtNVR_IP, "设备IP格式不正确");
                return false;
            }
            if (speNVR_Port.Value < 0 || speNVR_Port.Value > 65535)
            {
                base.errorPop.SetError(speNVR_Port, "端口超出范围");
                return false;
            }
            if (string.IsNullOrEmpty(txtNVR_UserName.Text))
            {
                base.errorPop.SetError(txtNVR_UserName, "用户名不能为空");
                return false;
            }
            int pwd = -1;
            if (string.IsNullOrEmpty(txtNVR_Pwd.Text) || !int.TryParse(txtNVR_Pwd.Text, out pwd) || pwd < 0)
            {
                base.errorPop.SetError(txtNVR_Pwd, "密码必须为数字,不能为空");
                return false;
            }
            // 连接
            NET_DVR_DEVICEINFO_V30 dev = new NET_DVR_DEVICEINFO_V30();
            int lUserID = HCNetSDK.NET_DVR_Login_V30(txtNVR_IP.Text, (ushort)speNVR_Port.Value, txtNVR_UserName.Text, txtNVR_Pwd.Text, out dev);
            if (lUserID == -1)
            {
                int errCode;
                var err = HCNetSDK.NET_DVR_GetErrorMsg(out errCode);
                lblStatus.Text = string.Format("登录DVS失败，错误码: {0}\r\n错误信息:{1}", errCode, err);
                return false;
            }
            lblStatus.Text = "登录DVS成功";
            return true;
        }
    }
}
